package com.steven.im.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.steven.im.R;
import com.steven.im.base.BaseFragment;
import com.steven.im.entity.IMContacts;
import com.steven.im.adapter.IMListViewAdapter;
import com.steven.im.ui.activity.IMAddFriendsActivity;
import com.steven.im.ui.activity.IMGroupListActivity;
import com.steven.im.ui.activity.IMNewFriendsActivity;
import com.steven.im.ui.activity.IMSearchActivity;
import com.steven.im.ui.activity.IMSystemNoticeActivity;
import com.steven.im.ui.activity.IMUserInfoActivity;
import com.steven.im.widget.QuickIndexBar;
import com.steven.im.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

public class IMContactsFragment extends BaseFragment {
    @BindView(R.id.quickIndexBar)
    QuickIndexBar quickIndexBar;
    @BindView(R.id.listview)
    AutoScrollListView listview;
    @BindView(R.id.currentWord)
    TextView currentWord;
    @BindView(R.id.view_status_bar_place)
    View statusBarPlace;
    @BindView(R.id.title_left)
    ImageView titleLeft;
    @BindView(R.id.title_mid)
    TextView titleMid;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    private boolean isFirst = true;//默认第一次加载
    private ArrayList<IMContacts> friends = new ArrayList<>();
    private boolean isScale = false;
    private Handler handler = new Handler();

    @SuppressLint("InflateParams")
    @Override

    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_contacts, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        titleLeft.setVisibility(View.GONE);
        titleMid.setText("联系人");
        ivTitleRight.setImageResource(R.drawable.icon_add);
        ivTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加朋友
                startActivity(new Intent(getActivity(), IMAddFriendsActivity.class));
            }
        });

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && isFirst) {
            isFirst = false;
            loadData();
        }
    }

    private void loadData() {
// 虚拟数据
        friends.add(new IMContacts("李伟"));
        friends.add(new IMContacts("张三"));
        friends.add(new IMContacts("阿三"));
        friends.add(new IMContacts("阿四"));
        friends.add(new IMContacts("段誉"));
        friends.add(new IMContacts("段正淳"));
        friends.add(new IMContacts("张三丰"));
        friends.add(new IMContacts("陈坤"));
        friends.add(new IMContacts("林俊杰1"));
        friends.add(new IMContacts("陈坤2"));
        friends.add(new IMContacts("王二a"));
        friends.add(new IMContacts("林俊杰a"));
        friends.add(new IMContacts("张四"));
        friends.add(new IMContacts("林俊杰"));
        friends.add(new IMContacts("王二"));
        friends.add(new IMContacts("王二b"));
        friends.add(new IMContacts("赵四"));
        friends.add(new IMContacts("杨坤"));
        friends.add(new IMContacts("赵子龙"));
        friends.add(new IMContacts("杨坤1"));
        friends.add(new IMContacts("李伟1"));
        friends.add(new IMContacts("宋江"));
        friends.add(new IMContacts("宋江1"));
        friends.add(new IMContacts("李伟3"));
        //2.对数据进行排序
        Collections.sort(friends);
        //3.设置Adapter
        listview.setAdapter(new IMListViewAdapter(getActivity(), friends));
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position > 0) {
                    startActivity(new Intent(getActivity(), IMUserInfoActivity.class));
                }
            }
        });
        quickIndexBar.setOnTouchLetterListener(new QuickIndexBar.OnTouchLetterListener() {
            @Override
            public void onTouchLetter(String letter) {
                if (letter.equals("↑")) {
                    listview.requestPositionToScreen(0,false);
                }
                //根据当前触摸的字母，去集合中找那个item的首字母和letter一样，然后将对应的item放到屏幕顶端
                for (int i = 0; i < friends.size(); i++) {
                    String firstWord = friends.get(i).getPinyin().charAt(0) + "";
                    if (!Character.isLetter(friends.get(i).getPinyin().charAt(0))) {
                        firstWord = "#";
                    }
                    if (letter.equals(firstWord)) {
                        //说明找到了，那么应该讲当前的item放到屏幕顶端
                        listview.setSelection(i + 1);
                        break;//只需要找到第一个就行
                    } else {
                        listview.setSelection(i + 1);
                    }
                }

                //显示当前触摸的字母
                showCurrentWord(letter);
            }
        });
        setViewClick();

        //通过缩小currentWord来隐藏
        ViewHelper.setScaleX(currentWord, 0);
        ViewHelper.setScaleY(currentWord, 0);
    }

    @SuppressLint("InflateParams")
    private void setViewClick() {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.im_contacts_header, null);
        listview.addHeaderView(view);
        (view.findViewById(R.id.rl_search)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), IMSearchActivity.class)); //搜索添加好友
            }
        });
        (view.findViewById(R.id.im_rlNewFriends)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//新朋友
                startActivity(new Intent(getActivity(), IMNewFriendsActivity.class));
            }
        });
        (view.findViewById(R.id.im_rlSystemNotice)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), IMSystemNoticeActivity.class));
            }
        });
        (view.findViewById(R.id.im_rlGroup)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), IMGroupListActivity.class));
            }
        });
    }


    protected void showCurrentWord(String letter) {
        currentWord.setText(letter);
        if (!isScale) {
            isScale = true;
            ViewPropertyAnimator.animate(currentWord).scaleX(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(0).start();
            ViewPropertyAnimator.animate(currentWord).scaleY(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(0).start();
        }

        //先移除之前的任务
        handler.removeCallbacksAndMessages(null);

        //延时隐藏currentWord
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//				currentWord.setVisibility(View.INVISIBLE);
                ViewPropertyAnimator.animate(currentWord).scaleX(0f).setDuration(0).start();
                ViewPropertyAnimator.animate(currentWord).scaleY(0f).setDuration(0).start();
                isScale = false;
            }
        }, 500);
    }

}
